package clrs

type RedBlackTreeNode struct {
	Key        int
	value      int
	isRed      bool
	isNIL      bool
	size       int32
	LeftChild  *RedBlackTreeNode
	Parent     *RedBlackTreeNode
	RightChild *RedBlackTreeNode
}

func NewRedBlackTreeNode(key int, value int, nilNode *RedBlackTreeNode) *RedBlackTreeNode {
	that := RedBlackTreeNode{
		Key:   key,
		value: value,
		isNIL: nilNode == nil,
	}

	defaultPointer := nilNode
	if nilNode == nil {
		defaultPointer = &that
	}

	that.reset(defaultPointer)
	return &that
}

func (that *RedBlackTreeNode) IsRed() bool {
	if that.isNIL {
		return false
	}

	return that.isRed
}

func (that *RedBlackTreeNode) SetRed(value bool) {
	if !that.isNIL {
		that.isRed = value
	}
}

func (that *RedBlackTreeNode) IsBlack() bool {
	if that.isNIL {
		return true
	}

	return !that.isRed
}

func (that *RedBlackTreeNode) SetBlack(value bool) {
	if !that.isNIL {
		that.isRed = !value
	}
}

func (that *RedBlackTreeNode) reset(nilNode *RedBlackTreeNode) {
	that.LeftChild = nilNode
	that.Parent = nilNode
	that.RightChild = nilNode
}

func (that *RedBlackTreeNode) CopyAttributesTo(to *RedBlackTreeNode) {
	to.Key = that.Key
	to.value = that.value
	to.isRed = that.isRed
}
